/*
 * 文 件 名 : HomeActivity.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2012-8-4
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.ui.chat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.fire8.dolphin.db.FriendDAO;
import com.fire8.dolphin.model.Friend;
import com.fire8.dolphin.ui.DolphinApplication;
import com.fire8.dolphin.ui.R;
import com.fire8.dolphin.ui.imgcache.AsynLoad;
import com.fire8.dolphin.utils.LogUtil;
import com.fire8.dolphin.web.RequestUtils;

/**
 * 主页面
 * 显示好友列表
 *
 * @author  lixg
 * @version [版本号,2012-8-4]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class FriendActivity extends Activity implements OnItemClickListener
{
    private ListView listView;
    private List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    private MyAdapter myAdapter;
    private FriendDAO dao;
    private DolphinApplication app;
    private AsynLoad asynLoad;
    private static boolean openFlag=false;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friend_list);
        app = (DolphinApplication)getApplication();
        dao = new FriendDAO(this);
        asynLoad = new AsynLoad();
        //注册广播接收器，收到广播就刷新好友列表
        IntentFilter filter = new IntentFilter("com.fire8.dolphin.ui.FriendActivity");
        filter.addAction("com.fire8.dolphin.ui.ChatActivity");
        this.registerReceiver(mReceiver, filter);
        
        listView = (ListView)findViewById(R.id.friend_list);
        listView.setOnItemClickListener(this);
        myAdapter = new MyAdapter(this);
        listView.setAdapter(myAdapter);
        findFriends();
    }
    private void findFriends()
    {
        list = dao.findList(null);
        myAdapter.notifyDataSetChanged();
    }
    
    
    @Override
    protected void onStart()
    {
        openFlag=true;
        super.onStart();
        //检查是否有新消息，如果有新消息，显示new图标
        findFriends();
    }
    @Override
    protected void onPause()
    {
        openFlag=false;
        super.onPause();
    }
    
    @Override
    protected void onDestroy()
    {
        openFlag=false;
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }
    
    public static boolean isOpen()
    {
        return openFlag;
    }
    
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id)
    {
        //点击用户，跳转到聊天窗口
        HashMap<String, String> map= list.get(position);
        String userId = map.get(Friend.C_USER_ID);
        LogUtil.d("发送的userId: "+userId);
        Intent intent = new Intent(this,ChatActivity.class);
        intent.putExtra("userId", userId);
        intent.putExtra(Friend.C_USER_NAME, map.get(Friend.C_USER_NAME));
        intent.putExtra(Friend.C_USER_HEAD, map.get(Friend.C_USER_HEAD));
        intent.putExtra(Friend.C_ACCOUNT, map.get(Friend.C_ACCOUNT));
        //intent.putExtra("from", "user");
        startActivity(intent);
    }
    
    
    
    public class MyAdapter extends BaseAdapter
    {
        private LayoutInflater mInflater;
        private Map<Integer,View> map;
        public MyAdapter(Context context)
        {
            this.mInflater = LayoutInflater.from(context);
            map = new HashMap<Integer, View>();
        }
        
        public int getCount()
        {
            return list.size();
        }
        
        public Object getItem(int position)
        {
            return null;
        }
        
        public long getItemId(int position)
        {
            return 0;
        }
        
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View rowView = map.get(position);
            ViewHolder holder = null; 
            if (rowView == null)
            {
                holder = new ViewHolder();
                rowView = mInflater.inflate(R.layout.friend_item, null);
                holder.imgHead = (ImageView)rowView.findViewById(R.id.img_head);
                holder.tvUserName = (TextView)rowView.findViewById(R.id.tv_user_name);
                //holder.tvPoint = (TextView)rowView.findViewById(R.id.tv_point);
                holder.tvRemark = (TextView)rowView.findViewById(R.id.tv_user_remark);
                holder.imgMsg = (ImageView)rowView.findViewById(R.id.img_msg);
                rowView.setTag(holder);
                map.put(position, rowView);
            }
            else
            {
                holder = (ViewHolder)rowView.getTag();
            }
            HashMap<String, String> map = list.get(position);
            
            String userName = map.get(Friend.C_USER_NAME);
            holder.tvUserName.setText(userName);
            holder.tvRemark.setText(map.get(Friend.C_REMARK));
            asynLoad.showimgAnsy(holder.imgHead,RequestUtils.IMG_URL+map.get(Friend.C_USER_ID)+".jpg");
            
            LogUtil.d("hasMsg:"+map.get(Friend.C_HAS_MSG));
            if("1".equals(map.get(Friend.C_HAS_MSG)))
            {
                //如果有新消息，则显示new 标签
                holder.imgMsg.setVisibility(View.VISIBLE);
                LogUtil.d("显示new 标签");
            }
            return rowView;
        }
        class ViewHolder
        {
            public ImageView imgHead;
            public TextView tvUserName;
            public TextView tvPoint;
            public TextView tvRemark;
            public ImageView imgMsg;
        }
    }
    private final BroadcastReceiver mReceiver = new BroadcastReceiver(){

        @Override
        public void onReceive(Context arg0, Intent arg1)
        {
            //刷新页面
            findFriends();
        }
        
    };
}
